package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.WarehouseTypeService;
import com.ray.depot.table.entity.WarehouseExample;
import com.ray.depot.table.entity.WarehouseType;
import com.ray.depot.table.entity.WarehouseTypeExample;
import com.ray.depot.table.entity.WarehouseTypeExample.Criteria;
import com.ray.depot.table.mapper.WarehouseMapper;
import com.ray.depot.table.mapper.WarehouseTypeMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class WarehouseTypeServiceImpl extends BaseService implements WarehouseTypeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(WarehouseTypeServiceImpl.class);
	
	@Autowired
	private WarehouseTypeMapper warehouseTypeMapper;
	@Autowired
	private WarehouseMapper warehouseMapper;

	@Transactional
	public Result<Object> addWarehouseType(WarehouseType params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setTypeId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=warehouseTypeMapper.insertSelective(params);
		if(count!=1){
			logger.error("WarehouseTypeServiceImpl=>addWarehouseType is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public WarehouseType queryWarehouseType(String id, LoginUser user) {
		return warehouseTypeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateWarehouseType(WarehouseType params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=warehouseTypeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("WarehouseTypeServiceImpl=>editWarehouseType is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteWarehouseType(String id, LoginUser user) {
    	//判断是否存在该类型的仓库
    	WarehouseType record = queryWarehouseType(id, user);
		if (existWarehouse(record, user)) {
			return new Result<Object>(-1, "存在改类型的仓库,仓库类型不能删除.");
		}
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		WarehouseType params=new WarehouseType();
		params.setTypeId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=warehouseTypeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("WarehouseTypeServiceImpl=>deleteWarehouseType is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    


	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<WarehouseType> queryWarehouseType(WarehouseType params, PageDto<WarehouseType> page, LoginUser user) {
		WarehouseTypeExample example=new WarehouseTypeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(warehouseTypeMapper.countByExample(example));
		page.setRecords(warehouseTypeMapper.selectByExample(example));
		return page;
	}


	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<WarehouseType> queryWarehouseType(WarehouseType warehouseType, LoginUser user) {
		WarehouseTypeExample example=new WarehouseTypeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		return warehouseTypeMapper.selectByExample(example);
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	private boolean existWarehouse(WarehouseType record, LoginUser user) {
		WarehouseExample example = new WarehouseExample();
		example.createCriteria()
		.andTenantIdEqualTo(user.getTenantId())
		.andIsDeleteEqualTo(DeleteEnum.USE.getValue())
		.andWarchouseTypeEqualTo(record.getTypeId());
		long count = warehouseMapper.countByExample(example);
		return count != 0;
	}
}
